package com.test.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class GenerateUtil {
    public static void generateTxt(){
        String fileName = "C:\\Users\\41636\\Desktop\\data.txt";  // 输出的CSV文件名
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            // 写入CSV文件的表头
            writer.write("id,title,content");
            writer.newLine();

            // 生成一百万条数据
            for (int i = 1; i <= 10000000; i++) {
                int id = i; // id 从 1 到 1000000
                String title = "Title " + i;  // 累加的标题
                String content = generateRandomContent();  // 随机生成的内容

                // 写入CSV文件
                writer.write(id + "," + title + "," + content);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("数据生成完成，已保存到 " + fileName);
    }

    public static void main(String[] args) {
        generateTxt();
    }

    // 生成随机内容的示例方法
    private static String generateRandomContent() {
        Random random = new Random();
        StringBuilder content = new StringBuilder();

        // 随机生成 100 个字符的内容
        for (int i = 0; i < 100; i++) {
            char randomChar = (char) (random.nextInt(26) + 'a');  // 生成小写字母
            content.append(randomChar);
        }

        return content.toString();
    }
}
